'use strict';

angular.module('myApp.mycards', []).controller('mycardsCtrl', ['$scope','$http','$routeParams',
	function($scope,$http,$routeParams){

	$scope.go_back = function(){
		window.location.href = '#/shop';
	};
	var p =2;
	var con = 2;
	$scope.has_coupon =true;
	$scope.shop_list = [];
	$scope.scroll_switch = false;


	$scope.delete_coupon = function(coupon){
		console.log(coupon.id);
		if (confirm("确定删除么?")) {
		    $http.delete('/consumer/'+userid+'/coupon/'+coupon.id).success(function(data){
		    	if (!data.error) {	
		    		alert("删除成功");
					window.location.reload();
		    	};
		    });	
		};	
	};
	if ($routeParams.id == 0) {
		$scope.right = true;
		$scope.name = "全部";
	    $scope.content = function(con){
			$scope.isDate = con;
	    	$scope.shop_list = [];
	    	$scope.scroll_switch=false;
	    	$http.get('/consumer/'+userid+'/coupon?condition='+con+'&page=1').success(function(data){
		    	if (!data.error) {	
		    		console.log(data);
		    		if (data.result.data.length == 0) {
		    			$scope.has_coupon = false;
		    		}else{
		    			$scope.has_coupon = true;
		    		};
	    			for (var i = 0; i < data.result.data.length; i++) {
	    				$scope.shop_list.push(data.result.data[i]);
	    			};
	    			$scope.coupons =$scope.shop_list;
		    	};
		    });	   	   	
	    	$scope.myPage = function(){
		    	if($scope.scroll_switch) return; 	
			    $http.get('/consumer/'+userid+'/coupon?condition='+con+'&page='+p).success(function(data){
			    	if (!data.error) {	
			    		console.log(data);
			    		if (p>data.result.last_page) {
			    			$scope.scroll_switch = true;
			    		}else{
			    			for (var i = 0; i < data.result.data.length; i++) {
			    				$scope.shop_list.push(data.result.data[i]);
			    			};
				    		$scope.coupons =$scope.shop_list;
				    		console.log(p);
				    		p++;
			    		};
			    	};
			    });	   	
			};	    	
		};	
		$scope.content(2);	  		
	}else{
		$scope.right = false;
	    $http.get('/seller/'+$routeParams.id).success(function(data){
	    	if (!data.error) {
	    		$scope.shop = data.result;
	    	};
	    });	
	    $scope.content = function(con){
	    	$scope.shop_list = [];
	    	$scope.isDate = con;
	    	console.log(con);
	    	$scope.scroll_switch=false;
	    	$http.get('/consumer/'+userid+'/coupon?condition='+con+'&seller_id='+$routeParams.id+'&page=1').success(function(data){
		    	if (!data.error) {	
		    		if (data.result.data.length ==0) {
		    			$scope.has_coupon = false;
		    		}else{
		    			$scope.has_coupon = true;
		    		};
	    			for (var i = 0; i < data.result.data.length; i++) {
	    				$scope.shop_list.push(data.result.data[i]);
	    			};
	    			$scope.coupons =$scope.shop_list;
		    	};
		    });	   	
	    	$scope.myPage = function(){
		    	if($scope.scroll_switch) return; 	
			    $http.get('/consumer/'+userid+'/coupon/condition?seller_id='+$routeParams.id+'&content='+con+'&page='+p).success(function(data){
			    	if (!data.error) {	
			    		console.log(data);
			    		if (p>data.result.last_page) {
			    			$scope.scroll_switch = true;
			    		}else{
			    			for (var i = 0; i < data.result.data.length; i++) {
			    				$scope.shop_list.push(data.result.data[i]);
			    			};
				    		$scope.coupons =$scope.shop_list;
				    		console.log(p);
				    		p++;
			    		};
			    	};
			    });	   	
			};
		};	
		$scope.content(2);		
	};
}]);